module.exports = (EdtApi) => {
    "use strict";
    let chatId = "", count = 0;
    function isGroup() {
        return false
    }
    async function getChatId(times = 0) {
        let url = window.location.href;
        const regex = /\/messages\/(\d+-\d+|\d+)\/?/;
        const match = regex.exec(url);
        let id = '';
        try {
            if (match) id = match[1];
        } catch (error) {
            console.log('获取对话id失败', error)
        }
        if (id && id != chatId) return id;
        await EdtApi.sleep(50);
        if (times > 10) return id;
        times++;
        return getChatId(times);
    }
    return {
        config: {
            friendList: 'aside nav div.messageList',
            chat: 'main>div.chat',
            input: 'main>div.chat form>textarea',
            msgBox: 'main>div.chat div[aria-live="polite"][role="log"]',
            inMsg: 'div.msgHelper>div.msg:not(.C($c-ds-text-chat-bubble-send)):not(.trans-inited)',
            outMsg: 'div.msgHelper>div.msg.C($c-ds-text-chat-bubble-send):not(.trans-inited)',
            allMsg: "div.msgHelper>div.msg",
            sendBtn: 'main form>button[type="submit"]',
            setting: "main>div.chat form",
            tips: {
                in: 'main>div.chat a',
                out: 'main>div.chat div.BdT'
            },
            crm: {
                // listener: '#LeftColumn-main',
                top: 'main>div.chat a>div:nth-child(2)',
                left: ""
            },
            placeholder: {
                selector: "main>div.chat form>textarea",
                attr: "placeholder"
            }
        },
        getUnread() {
            return count;
        },
        getChatInfo() {
            return new Promise(async (resolve) => {
                chatId = await getChatId();
                resolve({ id: chatId, isGroup: isGroup() });
            });
        },
        getChatTitle(chatEl = document) {
            let titleEl = chatEl.querySelector('a>div:nth-child(2)');
            return titleEl?.textContent || "";
        },
        getText(msgBox) {
            return msgBox?.textContent || "";
        },
        getIptMsg(inputEl) {
            return inputEl?.value || "";
        },
        getUserInfo() {
            try {
                let userEl = document.querySelector('aside nav a[href="/app/profile"]');
                let avatarEl = userEl?.querySelector('div>div[aria-hidden="true"]');
                let avatar = avatarEl?.style.backgroundImage || "";
                let nameEl = userEl?.querySelector("h2");
                let userName = nameEl?.textContent || "";
                return {
                    avatar,
                    userName,
                    account: ""
                };
            } catch (error) {
                return null;
            }
        }
    }
};